package com.pactera.madp.cp.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pactera.madp.cp.api.dto.crm.CrmCustomQueryConditionDTO;
import com.pactera.madp.cp.api.entity.crm.WorkCustomInfoEntity;
import com.pactera.madp.cp.api.vo.AddCustomerVO;
import com.pactera.madp.cp.common.constant.Constants;
import com.pactera.madp.cp.common.util.sync.SyncUtils;
import com.pactera.madp.cp.mapper.AddCustomerMapper;
import com.pactera.madp.cp.mapper.crm.WorkCustomInfoMapper;
import com.pactera.madp.cp.service.WorkCustomInfoService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
@AllArgsConstructor
public class WorkCustomInfoServiceImpl extends ServiceImpl<WorkCustomInfoMapper, WorkCustomInfoEntity> implements WorkCustomInfoService{

    private final WorkCustomInfoMapper workCustomInfoMapper;
    private final AddCustomerMapper addCustomerMapper;

    @Override
    public List<WorkCustomInfoEntity> listAll() {
        List<WorkCustomInfoEntity> all = new ArrayList<>();
        List<String> tableNames = SyncUtils.getTableNames(Constants.TP_WORK_CUSTOM_INFO, Constants.TC_CUSTOM_INFO);
        tableNames.forEach(t -> {
            List<WorkCustomInfoEntity> entityList = workCustomInfoMapper.list(t);
            if (!entityList.isEmpty()) {
                all.addAll(entityList);
            }
        });
        return all;
    }

    @Override
    public boolean removeAll() {
        List<String> tableNames = SyncUtils.getTableNames(Constants.TP_WORK_CUSTOM_INFO, Constants.TC_CUSTOM_INFO);
        tableNames.forEach(t -> workCustomInfoMapper.removeAll(t));
        return true;
    }

    @Override
    public boolean batchSaveAll(List<WorkCustomInfoEntity> entityList) {
        Map<String, List<WorkCustomInfoEntity>> data = specialHandle(entityList);
        if (ObjectUtil.isNull(data)) {
            return false;
        }
        data.forEach((k, v) -> workCustomInfoMapper.batchSaveOrUpdate(k, v));
        return true;
    }

    @Override
    public List<WorkCustomInfoEntity> listByCondition(CrmCustomQueryConditionDTO dto) {
        List<WorkCustomInfoEntity> all = new ArrayList<>();
        List<String> tableNames = SyncUtils.getTableNames(Constants.TP_WORK_CUSTOM_INFO, Constants.TC_CUSTOM_INFO);
        tableNames.forEach(t -> {
            List<WorkCustomInfoEntity> entityList = workCustomInfoMapper.listByCondition(t,dto);
            if (!entityList.isEmpty()) {
                all.addAll(entityList);
            }
        });
        return all;
    }

    @Override
    public Integer countInCustMgrNoAndMobile(List<String> custMgrNos, List<String> mobiles) {
        Integer totalCount = 0;
        List<String> tableNames = SyncUtils.getTableNames(Constants.TP_WORK_CUSTOM_INFO, Constants.TC_CUSTOM_INFO);
        for (String tableName : tableNames) {
            Integer count = workCustomInfoMapper.countInCustMgrNoAndMobile(tableName,custMgrNos,mobiles);
            totalCount += count;
        }
        return totalCount;
    }

    @Override
    public Integer countInOrgCodes(List<String> orgCodes) {
        Integer totalCount = 0;
        List<String> tableNames = SyncUtils.getTableNames(Constants.TP_WORK_CUSTOM_INFO, Constants.TC_CUSTOM_INFO);
        for (String tableName : tableNames) {
            Integer count = workCustomInfoMapper.countInOrgCodes(tableName,orgCodes);
            totalCount += count;
        }
        return totalCount;
    }

    @Override
    public Integer countInOrgCodesAndMgrNos(List<String> orgCodes, List<String> wxUserIds) {
        Integer totalCount = 0;
        List<String> tableNames = SyncUtils.getTableNames(Constants.TP_WORK_CUSTOM_INFO, Constants.TC_CUSTOM_INFO);
        for (String tableName : tableNames) {
            Integer count = workCustomInfoMapper.countInOrgCodesAndMgrNos(tableName,orgCodes,wxUserIds);
            totalCount += count;
        }
        return totalCount;
    }

    @Override
    public Integer countInMobiles(List<String> mobiles) {
        Integer totalCount = 0;
        List<String> tableNames = SyncUtils.getTableNames(Constants.TP_WORK_CUSTOM_INFO, Constants.TC_CUSTOM_INFO);
        for (String tableName : tableNames) {
            Integer count = workCustomInfoMapper.countInMobiles(tableName,mobiles);
            totalCount += count;
        }
        return totalCount;
    }

    @Override
    public List<Map<String,String>> getCustNameAndAssetsLevelByMobile(String mobile) {
        ArrayList<Map<String,String>> mapList = new ArrayList<>();
        List<String> tableNames = SyncUtils.getTableNames(Constants.TP_WORK_CUSTOM_INFO, Constants.TC_CUSTOM_INFO);
        for (String tableName : tableNames) {
            Map<String, String> map = workCustomInfoMapper.getCustNameAndAssetsLevelByMobile(tableName, mobile);
            mapList.add(map);
        }
        return mapList;
    }

    @Override
    public List<Map<String,String>> getCustNameAndAssetsLevelByMobiles(List<String> mobiles) {
        return workCustomInfoMapper.getCustNameAndAssetsLevelByMobileNew(mobiles);
    }

    @Override
    public Page<AddCustomerVO> getAddCustomerVOPage(Page page,String state,List<String> wxUserIds) {
        return workCustomInfoMapper.getAddCustomerVOPage(page,state,wxUserIds);
    }

    @Override
    public List<AddCustomerVO> addCustomerVOList(String state, List<String> wxUserIds, Integer tableSort, Long limitStart, Long size) {
        List<AddCustomerVO> list;
        String tableName;
        if (tableSort != 10) {
            tableName = "cp_work_custom_info_" + tableSort;
            if (state != null && state.equals(1)) {
                list = workCustomInfoMapper.list1(wxUserIds, tableName, limitStart, size);
            } else  if (state != null && state.equals(2)) {
                list = workCustomInfoMapper.list2(wxUserIds, tableName, limitStart, size);
            } else  if (state != null && state.equals(3)) {
                list = workCustomInfoMapper.list3(wxUserIds, tableName, limitStart, size);
            } else {
                list = workCustomInfoMapper.list0(wxUserIds, tableName, limitStart, size);
            }
        } else {
            list = addCustomerMapper.list0(limitStart, size, state);
        }
        return list;
    }

    @Override
    public List<AddCustomerVO> getAddCustomerVOListByCustMgrNo(String custMgrNo, List<String> wxUserIds) {
//        return workCustomInfoMapper.getAddCustomerVOListByCustMgrNo(custMgrNo,wxUserIds);
        List<AddCustomerVO> vos = workCustomInfoMapper.getAddCustomerVOListByCustMgrNoNew(custMgrNo,wxUserIds);
        return vos;
    }

    @Override
    public List<Map<String, Long>> count(String state, List<String> wxUserIds) {
        List<Map<String, Long>> list;
        if (state != null && state.equals("1")) {
            list = workCustomInfoMapper.count1(wxUserIds);
        } else if (state != null && state.equals("2")) {
            list = workCustomInfoMapper.count2(wxUserIds);
        } else if (state != null && state.equals("3")) {
            list = workCustomInfoMapper.count3(wxUserIds);
        } else {
            list = workCustomInfoMapper.count(wxUserIds);
        }
        return list;
    }

    /**
     * 分表存储
     * @param entityList
     * @return
     */
    private Map<String, List<WorkCustomInfoEntity>> specialHandle(List<WorkCustomInfoEntity> entityList) {
        if (CollUtil.isEmpty(entityList)) {
            return null;
        }
        Map<String, List<WorkCustomInfoEntity>> data = new HashMap<>();
        entityList.forEach(e -> {
            String tableName = SyncUtils.getTableName(e.getCustNo(), Constants.TP_WORK_CUSTOM_INFO, Constants.TC_CUSTOM_INFO);
            List<WorkCustomInfoEntity> list = data.get(tableName);
            if (ObjectUtil.isNull(list)) {
                list = new ArrayList<>();
                list.add(e);
                data.put(tableName, list);
            } else {
                list.add(e);
            }
        });
        return data;
    }

}
